Skip to content

Add log buffering docs #46232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

evgenyfedorov2
Copy link
Member

@evgenyfedorov2 evgenyfedorov2 commented May 16, 2025

@dotnetrepoman dotnetrepoman bot added this to the May 2025 milestone May 16, 2025
@evgenyfedorov2 evgenyfedorov2 force-pushed the users/evgenyfedorov2/log_buffering_doc branch 5 times, most recently from 1ad5e4b to b8fb0b7 Compare May 16, 2025 15:45
@evgenyfedorov2 evgenyfedorov2 force-pushed the users/evgenyfedorov2/log_buffering_doc branch from b8fb0b7 to 5cbf2fc Compare May 16, 2025 16:05
@evgenyfedorov2 evgenyfedorov2 marked this pull request as ready for review May 16, 2025 16:15
@evgenyfedorov2 evgenyfedorov2 requested review from IEvangelist and a team as code owners May 16, 2025 16:15
Copy link
Contributor

@Rageking8 Rageking8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple typo and grammatical suggestions.

@evgenyfedorov2 evgenyfedorov2 requested a review from Rageking8 May 20, 2025 06:15
@evgenyfedorov2
Copy link
Member Author

@IEvangelist @gewarren may I get it reviewed, please? Thanks!

@evgenyfedorov2 evgenyfedorov2 requested review from gewarren and Rageking8 and removed request for Rageking8 May 23, 2025 15:33
Copy link
Contributor

@gewarren gewarren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this. I left some suggestions.

Comment on lines 366 to +368
- name: Log Sampling
href: ../../core/extensions/log-sampling.md
- name: Log Buffering
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- name: Log Sampling
href: ../../core/extensions/log-sampling.md
- name: Log Buffering
- name: Log sampling
href: ../../core/extensions/log-sampling.md
- name: Log buffering

Comment on lines 1099 to +1101
- name: Log Sampling
href: ../core/extensions/log-sampling.md
- name: Log Buffering
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- name: Log Sampling
href: ../core/extensions/log-sampling.md
- name: Log Buffering
- name: Log sampling
href: ../core/extensions/log-sampling.md
- name: Log buffering


Buffered logs are stored in temporary circular buffers in process memory, and the following conditions apply:

- If the buffer is full, the oldest logs will be dropped and never emitted.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- If the buffer is full, the oldest logs will be dropped and never emitted.
- If the buffer is full, the oldest logs are dropped and never emitted.

Buffered logs are stored in temporary circular buffers in process memory, and the following conditions apply:

- If the buffer is full, the oldest logs will be dropped and never emitted.
- If you want to emit the buffered logs, you can call `Flush()` on the `GlobalLogBuffer` or `PerRequestLogBuffer` class.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- If you want to emit the buffered logs, you can call `Flush()` on the `GlobalLogBuffer` or `PerRequestLogBuffer` class.
- If you want to emit the buffered logs, you can call <xref:Microsoft.Extensions.Diagnostics.Buffering.LogBuffer.Flush> on the <xref:Microsoft.Extensions.Diagnostics.Buffering.GlobalLogBuffer> or <xref:Microsoft.Extensions.Diagnostics.Buffering.PerRequestLogBuffer> class.

There are two buffering strategies available:

- Global buffering: Buffers logs across the entire application.
- Per-request buffering: Buffers logs for each individual HTTP request if available, otherwise - buffers to the global buffer.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Per-request buffering: Buffers logs for each individual HTTP request if available, otherwise - buffers to the global buffer.
- Per-request buffering: Buffers logs for each individual HTTP request if available; otherwise, buffers to the global buffer.


Log buffering offers a trade-off between memory usage and log storage costs. Buffering logs in memory allows you to:

1. Selectively emit logs based on runtime conditions.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
1. Selectively emit logs based on runtime conditions.
1. Selectively emit logs based on run-time conditions.

- Set appropriate buffer size limits based on your application's memory constraints.
- Use per-request buffering for web applications to isolate logs by request.
- Configure auto-flush duration carefully to balance memory usage and log availability.
- Implement explicit flush triggers for important events (errors, warnings, etc.).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Implement explicit flush triggers for important events (errors, warnings, etc.).
- Implement explicit flush triggers for important events (such as errors and warnings).


- Log buffering is not supported in .NET 8 and earlier versions.
- The order of logs is not guaranteed to be preserved. However, original timestamps are preserved.
- Custom configuration per each logging provider is not supported. Same configuration is used for all providers.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Custom configuration per each logging provider is not supported. Same configuration is used for all providers.
- Custom configuration per each logging provider is not supported. The same configuration is used for all providers.

- The order of logs is not guaranteed to be preserved. However, original timestamps are preserved.
- Custom configuration per each logging provider is not supported. Same configuration is used for all providers.
- Log scopes are not supported. This means that if you use the <xref:Microsoft.Extensions.Logging.ILogger.BeginScope%2A> method, the buffered log records will not be associated with the scope.
- Not all information of the original log record is preserved. Log buffering internally uses <xref:Microsoft.Extensions.Logging.Abstractions.BufferedLogRecord> class when flushing, and its following properties are always empty:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Not all information of the original log record is preserved. Log buffering internally uses <xref:Microsoft.Extensions.Logging.Abstractions.BufferedLogRecord> class when flushing, and its following properties are always empty:
- Not all information of the original log record is preserved. Log buffering internally uses <xref:Microsoft.Extensions.Logging.Abstractions.BufferedLogRecord> class when flushing, and the following of its properties are always empty:


## See also

- [Log Sampling](log-sampling.md)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- [Log Sampling](log-sampling.md)
- [Log sampling](log-sampling.md)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[New article]: Log buffering
3 participants